package com.erdos.cas.client;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;

import com.erdos.cas.entity.TokenVO;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class MesHttpClient {

	private static String IP ;	
	
	public MesHttpClient(){
		init();
	}
	
	/**
	 * 初始化各个参数
	 */
	private void init(){
		Properties p = new Properties();
		InputStream is = null;
		 try {
			  is = getClass().getResourceAsStream("mes-cas-profiles.properties");
			p.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(is!=null)
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		IP = p.getProperty("ip");
	}
	
	/**
	 * 用户登录将会调用此方法
	 * @param name 用户名
	 * @param password 用户密码
	 * */
	@SuppressWarnings("unchecked")
	public List checkLogin(String name,String password){
//		PostMethod post = new PostMethod("http://localhost:8090/mes-cas-app/cas/applist/login.action");
		String checkLogin = "http://" + IP + "/mes-cas-app/cas/applist/login.action";
		PostMethod post = new PostMethod(checkLogin);
        post.setParameter("userName", name);
        post.setParameter("password",password);
        HttpClient httpClient = new HttpClient();
        List applist = new ArrayList();
        try {
			httpClient.executeMethod(post);
			String appListXml = post.getResponseBodyAsString().trim();
			if(appListXml.equals("noUser")){
				applist.add("noUser");
			}else if(appListXml.equals("noAppList")){
				applist.add("noAppList");
			}else if(appListXml.equals("logined")){
			    applist.add("logined");
			}else{
				applist = fromXml(appListXml);
			}
			return applist;
		} catch (HttpException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}finally{
			post.releaseConnection();
		}
	}
	
	/**
	 * 退出应用列表调用此方法
	 * @param tokenid token令牌码
	 */
	public boolean logout(String pin){
//		PostMethod post = new PostMethod("http://localhost:8090/mes-cas-app/cas/applist/removeToken.action");
		String logout = "http://" + IP + "/mes-cas-app/cas/applist/removeToken.action";
		PostMethod post = new PostMethod(logout);
		post.setParameter("pin",pin);
		HttpClient httpClient = new HttpClient();
		try {
			httpClient.executeMethod(post);
			String deleteFlag = post.getResponseBodyAsString().trim();
			if(deleteFlag.equals("true")){
				return true;
			}else{
				return false;
			}
		} catch (HttpException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}finally{
			post.releaseConnection();
		}
	}
	
	/**
	 * 检查token调用此方法
	 * @param tokenid token令牌码
	 */
	public boolean checkToken (String pin,String tokenId){
//		PostMethod post = new PostMethod("http://localhost:8090/mes-cas-app/cas/applist/checkToken.action");
		String checkToken = "http://" + IP + "/mes-cas-app/cas/applist/checkToken.action";
		PostMethod post = new PostMethod(checkToken);
		post.setParameter("pin", pin);
		post.setParameter("tokenid",tokenId);
		HttpClient httpClient = new HttpClient();
		try {
			httpClient.executeMethod(post);
			String checkFlag = post.getResponseBodyAsString().trim();
			if(checkFlag.equals("true")){
				return true;
			}else {
				return false;
			}
		} catch (HttpException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}finally{
			post.releaseConnection();
		}
	}
	
	/**
	 * 将xml格式化的应用列表转换成List形式上
	 * @param applistXml
	 */
	@SuppressWarnings("unchecked")
	private List fromXml(String appListXml){
		XStream stream = new XStream(new DomDriver());
		stream.alias("token", TokenVO.class);
		TokenVO[] token = (TokenVO[]) stream.fromXML(appListXml);
		List tokens = new ArrayList();
		for(int i = 0;i<token.length;i++){
			tokens.add(token[i]);
		}
		TokenVO tokenvo = (TokenVO) tokens.get(0);
		return tokens;
	}
	
	/**
	 * 保存用户访问过的应用列表
	 * @param loginId 
	 * @param appName
	 * @author anpenglong
	 * @since  2008-7-24
	 */
	public void loginApp(String loginId,String appName){
//		PostMethod post = new PostMethod("http://localhost:8090/mes-cas-app/cas/applist/recordApp.action");
		String loginApp = "http://" + IP + "/mes-cas-app/cas/applist/recordApp.action";
		PostMethod post = new PostMethod(loginApp);
		post.setParameter("pin", loginId);
		post.setParameter("appName", appName);
		HttpClient httpClient = new HttpClient();
		try {
			httpClient.executeMethod(post);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			post.releaseConnection();
		}
	}
}
